<template>
    <div class="my">
       <!-- 已登录头部 -->
      <div class="header user-info" v-if="user">
          <div class="base-info">
            <div class="left">
              <van-image
                class="avatar"
                fit="cover"
                round
                :src="userinfo.photo"
              />
              <span class="name">{{userinfo.name}}</span>
            </div>
            <div class="right">
              <van-button size="mini" round to="/user/profile">编辑资料</van-button>
            </div>
          </div>
           <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userinfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userinfo.fans_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userinfo.follow_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userinfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

      <!-- 未登录头部 -->
      <div class="header not-login" v-else>
          <!-- 点击路由跳转至登录页面 -->
          <div class="login-btn" @click="$router.push('/login')"> 
            <img src="~@/assets/mobile.png" class="phone-img">
            <span  class="text">登录 / 注册</span>
          </div>
      </div>

     

    <!-- 宫格导航 -->
    <van-grid clickable :column-num="2">
      <van-grid-item class="grid-item">
        <template #icon>
          <i class="toutiao toutiao-shoucang"></i>
        </template>
          <template #text>
             <span class="text">收藏</span>
          </template>
      </van-grid-item>
       <van-grid-item class="grid-item" >
        <template #icon>
          <i class="toutiao toutiao-lishi"></i>
        </template>
          <template #text>
             <span class="text">历史</span>
          </template>
      </van-grid-item>
    </van-grid>
      
      <!-- 退出按钮，如果用户登录才能看到此按钮 -->
      <van-cell title="消息通知" is-link  />
      <van-cell title="小智同学" is-link  />
      <van-cell title="退出登录"  class="loginbtn" clickable v-if="user"
        @click="quitbtn"
      />
      
    </div>
</template>

<script>
// 映射user数据，记录用户是否登录
import {mapState} from 'vuex'
// 导入获取用户信息的接口
import {getUserInfo} from '@/api/user'
export default {
  name: 'myindex',
  props: {},
  components: {},
  data () {
   return {
     // 用户信息
     userinfo:{} 
   }
  },
  computed: {
    ...mapState(['user']) // 取出vuex中的user数据到当前页面
  },
  watch: {},
  methods: {
    // 退出操作
    quitbtn(){
      this.$dialog.confirm({
        title: '退出',
        message: '确定要退出吗？',
      }).then(() => {
        // 清除登录状态(容器中的 user + 本地存储中的 user)
        this.$store.commit('setUser',null);
        }).catch(() => {
          console.log('点击了取消');
      });
    },
    // 拿到接口数据，需要返回一个promise对象 用asyc await接受
    async loadUserInfo(){
      try {
        const res = await getUserInfo()
        console.log(res.data.data);
        this.userinfo = res.data.data  // 接口数据赋值给当前对象

      }catch (err) {  
        console.log('获取数据失败',err);
      } 
    }
  },
  created () {
    console.log(this.user);
    // 如果用户登录就调用接口方法
    if(this.user) {
      this.loadUserInfo()
    }
  },
  mounted () {
  },
}
</script> 

<style scoped lang="less">
.user-info {
  .base-info {
    height: 231px;    padding:76px 32px 23px;
    box-sizing:border-box;
     display: flex;
      justify-content: space-between;
      align-items: center;
        .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 4px solid #fff;
          margin-right: 23px;
          border: 5px solid #fff;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }

  }
}
.my {
 .header {
    height: 361px;
    background: url('~@/assets/banner.png');
    background-size: cover;
 }
 .not-login {
   display:flex;
   justify-content: center;
   align-items: center;
   .login-btn {
     display: flex;
     flex-direction: column;
   }
   .phone-img {
     width: 132px;
     height: 132px;
     margin-bottom: 15px;
   }
   .text {
     font-size:28px;
     color: #fff;
   }
 }  
}
.data-stats {
  display: flex;
  .data-item {
    height: 130px;
    display: flex;
    flex: 1;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    color:#fff;
    .count {
      font-size: 36px;
    }
    .text {
      font-size: 23px;
    }
  }
}
.grid-item {
      i.toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 28px;
      }
    }
  .loginbtn {
    text-align: center;
    color: #d86262;
   
  }
</style>